﻿using AutoMapper;
using QYmall.Core.Model.Models;
using QYmall.Core.Model.ViewModels;

namespace QYmall.Core.Model.Profiles
{
    /// <summary>
    /// 商城实体映射
    /// </summary>
    public class ShopProfile : Profile
    {
        public ShopProfile()
        {
            //商品信息，将源数据映射到DTO
            CreateMap<ShopGoods, ShopGoodsDto>()
                .ForMember(
                    dest => dest.CategoryTitle,
                    opt =>
                    {
                        opt.MapFrom(src => string.Join(",", src.CategoryRelations.Select(x => x.ShopCategory!.Title).ToArray()));
                    }
                ).ForMember(
                    dest => dest.LabelTitle,
                    opt =>
                    {
                        opt.MapFrom(src => string.Join(",", src.LabelRelations.Select(x => x.ShopLabel!.Title).ToArray()));
                    }
                ).ForMember(
                    dest => dest.BrandTitle,
                    opt =>
                    {
                        opt.MapFrom(src => src.Brand != null ? src.Brand.Title : null);
                    }
                ).ForMember(
                    dest => dest.MerchantTitle,
                    opt =>
                    {
                        opt.MapFrom(src => src.Merchant != null ? src.Merchant.Title : null);
                    }
                );
            CreateMap<ShopGoods, ShopGoodsClientDto>()
               .ForMember(
                   dest => dest.CategoryTitle,
                   opt =>
                   {
                       opt.MapFrom(src => string.Join(",", src.CategoryRelations.Select(x => x.ShopCategory!.Title).ToArray()));
                   }
               ).ForMember(
                   dest => dest.LabelTitle,
                   opt =>
                   {
                       opt.MapFrom(src => string.Join(",", src.LabelRelations.Select(x => x.ShopLabel!.Title).ToArray()));
                   }
               ).ForMember(
                    dest => dest.BrandTitle,
                    opt =>
                    {
                        opt.MapFrom(src => src.Brand != null ? src.Brand.Title : null);
                    }
                ).ForMember(
                    dest => dest.MerchantTitle,
                    opt =>
                    {
                        opt.MapFrom(src => src.Merchant != null ? src.Merchant.Title : null);
                    }
                );
            CreateMap<ShopGoods, ShopGoodsEditDto>();
            CreateMap<ShopGoods, ShopGoodsListDto>();
            CreateMap<ShopGoodsProduct, ShopGoodsProductDto>();
            CreateMap<ShopGoodsProduct, ShopGoodsProductListDto>();
            CreateMap<ShopGoodsProduct, ShopGoodsProductClientDto>();
            CreateMap<ShopGoodsGroupPrice, ShopGoodsGroupPriceDto>();
            CreateMap<ShopGoodsFieldValue, ShopGoodsFieldValueDto>();
            CreateMap<ShopGoodsSku, ShopGoodsSkuDto>();
            CreateMap<ShopGoodsAlbum, ShopGoodsAlbumDto>();
            //商品信息，将DTO映射到源数据
            CreateMap<ShopGoodsEditDto, ShopGoods>();
            CreateMap<ShopGoodsFieldValueDto, ShopGoodsFieldValue>();
            CreateMap<ShopGoodsProductDto, ShopGoodsProduct>();
            CreateMap<ShopGoodsGroupPriceDto, ShopGoodsGroupPrice>();
            CreateMap<ShopGoodsSkuDto, ShopGoodsSku>();
            CreateMap<ShopGoodsAlbumDto, ShopGoodsAlbum>();
            CreateMap<ShopCategorySkuDto, ShopCategorySku>();

            //商品类别，将源数据映射到DTO
            CreateMap<ShopCategory, ShopCategoryDto>();
            CreateMap<ShopCategory, ShopCategoryEditDto>();
            CreateMap<ShopCategoryGoodsRelation, ShopCategoryGoodsRelationDto>();
            //商品类别，将DTO映射到源数据
            CreateMap<ShopCategoryEditDto, ShopCategory>();
            CreateMap<ShopCategoryGoodsRelationDto, ShopCategoryGoodsRelation>();

            //商品标签，将源数据映射到DTO
            CreateMap<ShopLabel, ShopLabelDto>();
            CreateMap<ShopLabel, ShopLabelEditDto>();
            CreateMap<ShopLabelGoodsRelation, ShopLabelGoodsRelationDto>();
            //商品标签，将DTO映射到源数据
            CreateMap<ShopLabelEditDto, ShopLabel>();
            CreateMap<ShopLabelGoodsRelationDto, ShopLabelGoodsRelation>();

            //商品品牌，将源数据映射到DTO
            CreateMap<ShopBrand, ShopBrandDto>();
            CreateMap<ShopBrand, ShopBrandEditDto>();
            //商品品牌，将DTO映射到源数据
            CreateMap<ShopBrandEditDto, ShopBrand>();

            //商品规格，将源数据映射到DTO
            CreateMap<ShopSkus, ShopSkusDto>();
            CreateMap<ShopSkus, ShopSkusListDto>();
            CreateMap<ShopSkus, ShopSkusEditDto>();
            CreateMap<ShopSkus, ShopSkusChildrenDto>();
            //商品规格，将DTO映射到源数据
            CreateMap<ShopSkusEditDto, ShopSkus>();
            CreateMap<ShopSkusChildrenDto, ShopSkus>();

            //快递公司，将源数据映射到DTO
            CreateMap<ShopExpress, ShopExpressDto>();
            CreateMap<ShopExpress, ShopExpressEditDto>();
            //快递公司，将DTO映射到源数据
            CreateMap<ShopExpressEditDto, ShopExpress>();

            //配送方式，将源数据映射到DTO
            CreateMap<ShopDelivery, ShopDeliveryDto>();
            CreateMap<ShopDelivery, ShopDeliveryEditDto>();
            CreateMap<ShopDelivery, ShopDeliveryCartDto>();
            CreateMap<ShopDeliveryArea, ShopDeliveryAreaDto>();
            //配送方式，将DTO映射到源数据
            CreateMap<ShopDeliveryEditDto, ShopDelivery>();
            CreateMap<ShopDeliveryAreaDto, ShopDeliveryArea>();

            //扩展属性，将源数据映射到DTO
            CreateMap<ShopField, ShopFieldDto>();
            CreateMap<ShopField, ShopFieldEditDto>();
            CreateMap<ShopFieldOption, ShopFieldOptionDto>();
            //扩展属性，将DTO映射到源数据
            CreateMap<ShopFieldEditDto, ShopField>();
            CreateMap<ShopFieldOptionDto, ShopFieldOption>();

            //商品收藏，将源数据映射到DTO
            CreateMap<ShopGoodsFavorite, ShopGoodsFavoriteDto>();
            //商品收藏，将DTO映射到源数据
            CreateMap<ShopGoodsFavoriteEditDto, ShopGoodsFavorite>();

            //商品预售，将源数据映射到DTO
            CreateMap<ShopPresell, ShopPresellDto>()
                .ForMember(
                    dest => dest.MerchantTitle,
                    opt =>
                    {
                        opt.MapFrom(src => src.Merchant != null ? src.Merchant.Title : null);
                    }
                );
            CreateMap<ShopPresell, ShopPresellEditDto>();
            //商品预售，将DTO映射到源数据
            CreateMap<ShopPresellEditDto, ShopPresell>();

            //促销活动，将源数据映射到DTO
            CreateMap<ShopPromotion, ShopPromotionDto>()
                .ForMember(
                    dest => dest.MerchantTitle,
                    opt =>
                    {
                        opt.MapFrom(src => src.Merchant != null ? src.Merchant.Title : null);
                    }
                );
            CreateMap<ShopPromotion, ShopPromotionEditDto>();
            //促销活动，将DTO映射到源数据
            CreateMap<ShopPromotionEditDto, ShopPromotion>();

            //商品抢购，将源数据映射到DTO
            CreateMap<ShopSpeed, ShopSpeedDto>()
                .ForMember(
                    dest => dest.MerchantTitle,
                    opt =>
                    {
                        opt.MapFrom(src => src.Merchant != null ? src.Merchant.Title : null);
                    }
                );
            CreateMap<ShopSpeed, ShopSpeedEditDto>();
            //商品抢购，将DTO映射到源数据
            CreateMap<ShopSpeedEditDto, ShopSpeed>();

            //优惠券，将源数据映射到DTO
            CreateMap<ShopCoupon, ShopCouponDto>()
                .ForMember(
                    dest => dest.MerchantTitle,
                    opt =>
                    {
                        opt.MapFrom(src => src.Merchant != null ? src.Merchant.Title : null);
                    }
                );
            CreateMap<ShopCoupon, ShopCouponEditDto>();
            CreateMap<ShopCouponGoodsRelation, ShopCouponGoodsRelationDto>();
            CreateMap<ShopCouponHistory, ShopCouponHistoryDto>();
            CreateMap<ShopCouponHistory, ShopCouponHistoryEditDto>();
            CreateMap<ShopCouponHistory, ShopCouponHistoryCartDto>()
                .ForMember(
                    dest => dest.UseType,
                    opt =>
                    {
                        opt.MapFrom(src => src.ShopCoupon != null ? src.ShopCoupon.UseType : 0);
                    }
                ).ForMember(
                    dest => dest.Title,
                    opt =>
                    {
                        opt.MapFrom(src => src.ShopCoupon != null ? src.ShopCoupon.Title : null);
                    }
                ).ForMember(
                    dest => dest.Amount,
                    opt =>
                    {
                        opt.MapFrom(src => src.ShopCoupon != null ? src.ShopCoupon.Amount : 0);
                    }
                ).ForMember(
                    dest => dest.MinAmount,
                    opt =>
                    {
                        opt.MapFrom(src => src.ShopCoupon != null ? src.ShopCoupon.MinAmount : 0);
                    }
                ).ForMember(
                    dest => dest.StartTime,
                    opt =>
                    {
                        opt.MapFrom(src => src.ShopCoupon != null ? src.ShopCoupon.StartTime : new Nullable<DateTime>());
                    }
                ).ForMember(
                    dest => dest.EndTime,
                    opt =>
                    {
                        opt.MapFrom(src => src.ShopCoupon != null ? src.ShopCoupon.EndTime : new Nullable<DateTime>());
                    }
                );
            //优惠券，将DTO映射到源数据
            CreateMap<ShopCouponEditDto, ShopCoupon>();
            CreateMap<ShopCouponGoodsRelationDto, ShopCouponGoodsRelation>();
            CreateMap<ShopCouponHistoryEditDto, ShopCouponHistory>();

            //积分兑换，将源数据映射到DTO
            CreateMap<ShopConvert, ShopConvertDto>()
                .ForMember(
                    dest => dest.MerchantTitle,
                    opt =>
                    {
                        opt.MapFrom(src => src.Merchant != null ? src.Merchant.Title : null);
                    }
                );
            CreateMap<ShopConvert, ShopConvertEditDto>();
            CreateMap<ShopConvertHistory, ShopConvertHistoryDto>()
                .ForMember(
                    dest => dest.ConvertTitle,
                    opt =>
                    {
                        opt.MapFrom(src => src.ShopConvert!.Title);
                    }
                ).ForMember(
                    dest => dest.OrderNo,
                    opt =>
                    {
                        opt.MapFrom(src => src.Order!.OrderNo);
                    }
                );
            //积分兑换，将DTO映射到源数据
            CreateMap<ShopConvertEditDto, ShopConvert>();
            CreateMap<ShopConvertHistoryDto, ShopConvertHistory>();

            //购物车，将源数据映射到DTO
            CreateMap<ShopCart, ShopCartDto>();
        }
    }
}
